//=============================================================================
// System  : MatchKit
// File    : MatchExtensions.cs
// Author  : Fabio Bussu  (matchkit@fabiobussu.it)
// Updated : 04/16/2013
// Note    : Copyright 2013, Fabio Bussu, All rights reserved
//
// This code is published under the Microsoft Public License (Ms-PL). A copy
// of the license should be distributed with the code.  It can also be found
// at the project website: http://matchkit.codeplex.com. This notice, the
// author's name, and all copyright notices must remain intact in all
// applications, documentation, and source files.
//
// Version     Date     Who  Comments
// ============================================================================
// 1.0.0.0  04/16/2013   FB  First draft
//=============================================================================
using System;
using System.Collections.Generic;

namespace MatchKit
{
	/// <summary>
	/// Extends Match type with shortcut and utility methods.
	/// </summary>
	/// <threadsafety static="true" />
	/// <conceptualLink target="0a06b2a9-5f58-409a-ba62-639cfd77bca7" />
	public static class MatchExtensions
	{
		/// <summary>
		/// Finds recursively a child match given a capture name. The method returns only the first match found.
		/// </summary>
		/// <param name="match">Match to recursively search.</param>
		/// <param name="name">Capture name. This value cannot be null.</param>
		/// <returns>The match found. Null if not found.</returns>
		/// <remarks>
		/// The search performed on the capture name is case insensitive.
		/// </remarks>
		public static Match GetCapture(
			#if EM
			this 
			#endif
			Match match, string name)
		{
			return GetCapture(match, name, StringComparison.CurrentCultureIgnoreCase);
		}

		/// <summary>
		/// Finds recursively a child match given a capture name, specifying a name comparison type. 
		/// The method returns only the first match found.
		/// </summary>
		/// <param name="match">Match to recursively search.</param>
		/// <param name="name">Capture name. This value cannot be null.</param>
		/// <param name="comparisonType">Type of comparison.</param>
		/// <returns>The match found. Null if not found.</returns>
		public static Match GetCapture(
			#if EM
			this 
			#endif
			Match match, string name, StringComparison comparisonType)
		{
			if (match == null)
				throw new ArgumentNullException("match", "match cannot be null");
			if (name == null)
				throw new ArgumentNullException("name", "name cannot be null");

			if (string.Equals(match.CaptureName, name, comparisonType))
				return match;

			if (match.Children != null)
			{
				foreach (var child in match.Children)
				{
					var m = GetCapture(child, name, comparisonType);
					if (m != null)
						return m;
				}
			}
			return null;
		}

		/// <summary>
		/// Finds recursively children matches given a capture name. The method returns all the occurrences found.
		/// </summary>
		/// <param name="match">Match to recursively search.</param>
		/// <param name="name">Capture name. This value cannot be null.</param>
		/// <returns>The matches found. Null if not found.</returns>
		/// <remarks>
		/// The search performed on the capture name is case insensitive.
		/// </remarks>
		public static List<Match> GetCaptures(
			#if EM
			this 
			#endif
			Match match, string name)
		{
			return GetCaptures(match, name, StringComparison.CurrentCultureIgnoreCase);
		}

		/// <summary>
		/// Finds recursively children matches given a capture name, specifying a name comparison type. 
		/// The method returns all the occurrences found.
		/// </summary>
		/// <param name="match">Match to recursively search.</param>
		/// <param name="name">Capture name. This value cannot be null.</param>
		/// <param name="comparisonType">Type of comparison.</param>
		/// <returns>The matches found. Empty list if not found.</returns>
		public static List<Match> GetCaptures(
			#if EM
			this 
			#endif
			Match match, string name, StringComparison comparisonType)
		{
			if (match == null)
				throw new ArgumentNullException("match", "match cannot be null");
			if (name == null)
				throw new ArgumentNullException("name", "name cannot be null");

			var results = new List<Match>();
			GetCaptures(results, match, name, comparisonType);
			return results;
		}

		private static void GetCaptures(List<Match> results, Match match, string name, StringComparison comparisonType = StringComparison.CurrentCultureIgnoreCase)
		{
			if (string.Equals(match.CaptureName, name, comparisonType))
				results.Add(match);
			if (match.Children != null)
			{
				foreach (var child in match.Children)
				{
					GetCaptures(results, child, name, comparisonType);
				}
			}
		}

		/// <summary>
		/// Finds recursively children matches with a capture name. 
		/// The method returns all the occurrences found.
		/// </summary>
		/// <param name="match">Match to recursively search.</param>
		/// <returns>The captured matches found. Empty list if not found.</returns>
		public static List<Match> GetCaptures(
			#if EM
			this 
			#endif
			Match match)
		{
			if (match == null)
				throw new ArgumentNullException("match", "match cannot be null");
			var list = new List<Match>();
			GetCaptures(list, match);
			return list;
		}

		private static void GetCaptures(List<Match> results, Match match)
		{
			if (match.CaptureName != null)
			{
				results.Add(match);
			}
			if (match.Children != null)
			{
				foreach (var child in match.Children)
				{
					GetCaptures(results, child);
				}
			}
		}
	}
}
